const WpFile = require('./fileModel')
const ShareFile = require('./shareFileModel')
const Share = require('./shareModel')
const Source = require('./sourceModel')
const User = require('./userModel')
const Role = require('./role')
const Department = require('./department')
const Permission = require('./permission')
const SystemOption = require('./systemOption')
const CooperatDepartment = require('./cooperatDepartmentModel')
const RolePermission = require('./rolePermission')
const UserDepartment = require('./userDepartmentModel')
const Notice = require('./noticeModel')
const NoticeUser = require('./noticeUserModel')
const ScheduleTask = require('./scheduleTaskModel')
const SystemLog = require('./systemLogModel')
const ShareTo = require('./shareToModel')
const SourceAuth = require('./sourceAuthModel')
const BinSource = require('./binSourceModel')

Role.belongsToMany(Permission, {
  through: RolePermission,
  foreignKey: 'roleId'
})

Permission.belongsToMany(Role, {
  through: RolePermission,
  foreignKey: 'permissionId'
})

RolePermission.belongsTo(Permission, {
  foreignKey: 'permissionId'
})
Permission.hasMany(RolePermission, {
  foreignKey: 'permissionId'
})

User.belongsToMany(Department, {
  through: UserDepartment,
  foreignKey: 'userId'
  // as: 'departments'
})
Department.belongsToMany(User, {
  through: UserDepartment,
  foreignKey: 'departmentId'
  // as: 'users'
})

UserDepartment.belongsTo(Department, {
  foreignKey: 'departmentId'
})
Department.hasMany(UserDepartment, {
  foreignKey: 'departmentId'
})

User.hasMany(Share, { foreignKey: 'createUser' })
Share.belongsTo(User, { foreignKey: 'createUser' })

Share.hasMany(ShareTo, { foreignKey: 'share_id' })
ShareTo.belongsTo(Share, { foreignKey: 'share_id' })

ShareTo.belongsTo(User, { foreignKey: 'targetId' })
ShareTo.belongsTo(Department, { foreignKey: 'targetId' })
User.hasMany(ShareTo, { foreignKey: 'targetId' })
Department.hasMany(ShareTo, { foreignKey: 'targetId' })

// 外键roleId
User.belongsTo(Role, { foreignKey: 'role_id', as: 'role' })

// source通过外键连接user
Source.belongsTo(User, { foreignKey: 'createUser', as: 'user' })
// source通过外键链接file
Source.belongsTo(WpFile, { foreignKey: 'fileId', as: 'file' })
WpFile.hasMany(Source, { foreignKey: 'fileId', as: 'source' })

// source和分享表之间的关系
Source.hasOne(Share, { foreignKey: 'source_id' })
Share.belongsTo(Source, { foreignKey: 'source_id', as: 'source' })

// binSource通过sourceId链接source
BinSource.belongsTo(Source, { foreignKey: 'source_id', as: 'source' })
Source.hasOne(BinSource, { foreignKey: 'source_id', as: 'binSource' })

// binSource通过create_user链接用户信息
BinSource.belongsTo(User, { foreignKey: 'createUser', as: 'user' })

Department.hasMany(CooperatDepartment, {
  foreignKey: 'dep_id',
  as: 'copDepartments'
})
CooperatDepartment.belongsTo(Department, {
  foreignKey: 'dep_id',
  as: 'department'
})

// source与department的关系
Department.hasMany(Source, { foreignKey: 'targetId' })
// Source.belongsTo(Department, { foreignKey: 'targetId' })

Source.hasMany(SourceAuth, { foreignKey: 'sourceId' })
User.hasMany(SourceAuth, { foreignKey: 'targetId' })
Department.hasMany(SourceAuth, { foreignKey: 'targetId' })
SourceAuth.belongsTo(User, { foreignKey: 'targetId' })
SourceAuth.belongsTo(Department, { foreignKey: 'targetId' })

module.exports = {
  User,
  Share,
  Source,
  ShareFile,
  File: WpFile,
  Role,
  Department,
  Permission,
  SystemOption,
  CooperatDepartment,
  RolePermission,
  UserDepartment,
  Notice,
  NoticeUser,
  ScheduleTask,
  SystemLog,
  ShareTo,
  SourceAuth,
  BinSource
}
